package xyz.chenyongpeng.buyjj.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import xyz.chenyongpeng.buyjj.entity.MyOrder;
import xyz.chenyongpeng.buyjj.service.IOrderService;

import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/api/order")
@Slf4j
public class OrderController {

    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单记录
     * @param openid
     * @return
     */
    @GetMapping("/getOrder")
    public List<MyOrder> getList(String openid) {
        return orderService.list((Wrapper) new QueryWrapper<>().eq("openid", openid));
    }

    /**
     * 新增
     * @param order
     * @return
     */
    @PostMapping("/addOrder")
    public boolean addOrder(MyOrder order) {
        return orderService.save(order);
    }

    /**
     * 删除订单
     * @param order_id
     * @return
     */
    @GetMapping("/delOrder")
    public boolean delOrder(String order_id) {
        return orderService.remove((Wrapper) new QueryWrapper<>().eq("order_id", order_id));
    }

    /**
     * 查询该商品是否已经购买成功过
     * @param openid
     * @param good_id
     * @return
     */
    @GetMapping("/isBug")
    public boolean isBug(String openid,String good_id) {
        MyOrder one = orderService.getOne((Wrapper) new QueryWrapper<>().eq("openid", openid).eq("good_id", good_id)
        .eq("flag", "2"));
        if (one != null){
            return true;
        }
        return false;
    }

    @GetMapping("/paytest")
    public boolean updateFlag(){
        MyOrder myOrder = new MyOrder();
        myOrder.setFlag(2); // 支付成功
        return orderService.update(myOrder,(Wrapper) new UpdateWrapper<>().eq("order_id", "1"));
    }
}
